﻿Public Class TopPage
    Inherits BasePage

#Region "khai bao các event"

    Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
        Try
            Me.Header.Title = Me.AppSettings("DefaultPageTitle")

            Dim dsProductListHomePage As DataSet = GetProductListHomePage()
            Dim dsProductBestSeller As DataSet = GetProductBestSeller()

            ucProductList.DataSource = dsProductListHomePage
            ucProductBestSeller.DataSource = dsProductBestSeller
            Me.DataBind()
        Catch ex As Exception
            Server.Transfer("~/Pages/Error.aspx")
        End Try
    End Sub

    Private Sub ProductList_Init(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Init
        Try
            Dim master As LinhKienMayTinhMasterPage = DirectCast(Me.Page.Master, LinhKienMayTinhMasterPage)
            master.CssLinkAdd(ResolveUrl("~/Style/HomePageStyle.css"))            
            master.CssLinkAdd(ResolveUrl("~/Style/ProductListMetroStyle.css"))
            master.ScriptLinkAdd(ResolveUrl("~/Scripts/HomePageControl.js" + "?v=" + Guid.NewGuid().ToString("N")))
        Catch ex As Exception
            Server.Transfer("~/Pages/Error.aspx")
        End Try        
    End Sub

#End Region

#Region "khai bao các method"

    Protected Function GetProductListHomePage() As DataSet
        Dim xSql As String
        Dim ds As DataSet
        Dim dataAccess As DBAccess

        Try
            dataAccess = New DBAccess(ConfigurationManager.ConnectionStrings("ConStr").ConnectionString)

            xSql = ""
            xSql = xSql & vbCrLf & "SELECT TOP 12"
            xSql = xSql & vbCrLf & "		T_PRODUCT.CODE,"
            xSql = xSql & vbCrLf & "		T_PRODUCT.NAME          AS PRODUCT_NAME,"
            xSql = xSql & vbCrLf & "		T_PRODUCT.LINK_NAME     AS PRODUCT_LINK_NAME,"
            xSql = xSql & vbCrLf & "		T_PRODUCT.IMG_LINK,"
            xSql = xSql & vbCrLf & "		T_CATEGORY.NAME         AS CATEGORY_NAME,"
            xSql = xSql & vbCrLf & "		T_CATEGORY.LINK_NAME    AS CATEGORY_LINK_NAME,"
            xSql = xSql & vbCrLf & "		T_MANUFACTORY.NAME      AS MANUFACTORY_NAME,"
            xSql = xSql & vbCrLf & "		T_MANUFACTORY.LINK_NAME AS MANUFACTORY_LINK_NAME,"
            xSql = xSql & vbCrLf & "		ISNULL(T_PRODUCT.PRICE, 0) AS PRICE,"
            xSql = xSql & vbCrLf & "		T_PRODUCT.SPECIFICATION,"
            xSql = xSql & vbCrLf & "		T_PRODUCT.DESCRIPTION"
            xSql = xSql & vbCrLf & "FROM (T_PRODUCT LEFT JOIN T_CATEGORY ON"
            xSql = xSql & vbCrLf & "		T_PRODUCT.CATEGORY = T_CATEGORY.CODE)"
            xSql = xSql & vbCrLf & "LEFT JOIN T_MANUFACTORY"
            xSql = xSql & vbCrLf & "		ON T_PRODUCT.MANUFACTORY = T_MANUFACTORY.CODE"
            xSql = xSql & vbCrLf & "WHERE"
            xSql = xSql & vbCrLf & "        T_PRODUCT.DEL_FLG   = 0"
            xSql = xSql & vbCrLf & "AND		T_PRODUCT.HOME_PAGE_SHOW = 1"

            ds = dataAccess.GetDBDataset(xSql)

            Return ds
        Catch ex As Exception
            Throw
        End Try
    End Function

    Protected Function GetProductBestSeller() As DataSet
        Dim xSql As String
        Dim ds As DataSet
        Dim dataAccess As DBAccess

        Try
            dataAccess = New DBAccess(ConfigurationManager.ConnectionStrings("ConStr").ConnectionString)

            xSql = ""
            xSql = xSql & vbCrLf & "SELECT TOP 12"
            xSql = xSql & vbCrLf & "		T_PRODUCT.CODE,"
            xSql = xSql & vbCrLf & "		T_PRODUCT.NAME          AS PRODUCT_NAME,"
            xSql = xSql & vbCrLf & "		T_PRODUCT.LINK_NAME     AS PRODUCT_LINK_NAME,"
            xSql = xSql & vbCrLf & "		T_PRODUCT.IMG_LINK,"
            xSql = xSql & vbCrLf & "		T_CATEGORY.NAME         AS CATEGORY_NAME,"
            xSql = xSql & vbCrLf & "		T_CATEGORY.LINK_NAME    AS CATEGORY_LINK_NAME,"
            xSql = xSql & vbCrLf & "		T_MANUFACTORY.NAME      AS MANUFACTORY_NAME,"
            xSql = xSql & vbCrLf & "		T_MANUFACTORY.LINK_NAME AS MANUFACTORY_LINK_NAME,"
            xSql = xSql & vbCrLf & "		ISNULL(T_PRODUCT.PRICE, 0) AS PRICE,"
            xSql = xSql & vbCrLf & "		T_PRODUCT.SPECIFICATION,"
            xSql = xSql & vbCrLf & "		T_PRODUCT.DESCRIPTION"
            xSql = xSql & vbCrLf & "FROM (T_PRODUCT LEFT JOIN T_CATEGORY ON"
            xSql = xSql & vbCrLf & "		T_PRODUCT.CATEGORY = T_CATEGORY.CODE)"
            xSql = xSql & vbCrLf & "LEFT JOIN T_MANUFACTORY"
            xSql = xSql & vbCrLf & "		ON T_PRODUCT.MANUFACTORY = T_MANUFACTORY.CODE"
            xSql = xSql & vbCrLf & "WHERE"
            xSql = xSql & vbCrLf & "        T_PRODUCT.DEL_FLG   = 0"
            xSql = xSql & vbCrLf & "AND		T_PRODUCT.BEST_SELLER = 1"

            ds = dataAccess.GetDBDataset(xSql)

            Return ds
        Catch ex As Exception
            Throw
        End Try
    End Function

#End Region

End Class